import server from "@/api/request.js";
import qs from "qs";
import axios from "axios";
import { dict } from "@/utils/base";
import Vue from 'vue';

// const ip = dict.ADMIN_URL;
const API_URL = process.env.VUE_APP_BASE_URL;

//登录
export const login = data => {
  return server.post(API_URL + "/auth/login", qs.stringify(data));
};

export const httpPost = (url, data) => {
  return new Promise((reject, resolve) => {
      server.post(`${API_URL }${url}`, data)
        .then(resolue => {
          reject(resolue.data)
        })
        .catch(error => {
          resolve(error)
        });
  })
}
export const httpGet = (url, params) => {
  return new Promise((reject, resolve) => {
      server.get(`${API_URL }${url}`, { params })
        .then(resolue => {
          reject(resolue.data)
        })
        .catch(error => {
          resolve(error)
        });
  })
}
export const httpPut = (url, data) => {
  return new Promise((reject, resolve) => {
    server.put(`${API_URL }${url}`, data)
      .then(resolue => {
        reject(resolue.data)
      })
      .catch(error => {
        resolve(error)
      });
  })
}
export const httpDelete = (url, data) => {
  return new Promise((reject, resolve) => {
    server.delete(`${API_URL }${url}`, { data })
     .then(resolue => {
        reject(resolue.data)
      })
     .catch(error => {
        resolve(error)
      });
  })
}

export const httpPostFile = (url, params) => {
    return new Promise((reject, resolve) => {
        server({
            url: `${API_URL }${url}`,
            method: 'POST',
            data: params,
            responseType: 'blob',
            // onUploadProgress: (e) => {
            //   console.log(e, '444444');
            // },
            // onDownloadProgress: (e) => {
            //   console.log(e, '33333');
            // }
        }).then(resolue => {
            reject(resolue)
        }).catch(error => {
            resolve(error)
        });
    })
}
let http = {
  get: httpGet,
  post: httpPost,
  put: httpPut,
  delete: httpDelete,
  postFile: httpPostFile
}

Vue.prototype.$http = http

//地图border 压缩文件
export const getGeo = (url) => {
  return axios.get(url, {
    responseType: "arraybuffer",
  });
};

//地图加载本地数据
export const getJons = (data) => {
  return axios.get(data + ".json");
};

// 2级省市下拉联动
export const fileTypelist = (data) => {
  return server.post(`${API_URL}/area/list`, qs.stringify(data));
};

getJons, fileTypelist, (Vue.prototype.$http = http);
